package com.toutiao.model

import net.liftweb.json.{JsonAST,Extraction,Printer,parse}
import net.liftweb.json.JsonDSL._

import org.msgpack.ScalaMessagePack

import java.util.Date
import com.toutiao.issue.SimpleDate

import com.toutiao.score.Topic

case class RTp(
      i:String,
      c:String,u:String,
      v:Map[String,Double],s:Double,
      m:Map[String,Array[String]]
    )


object Converter {
  
  implicit val _ = net.liftweb.json.DefaultFormats
  
  
  def convertTopic(t:Topic) = {
    val n = new RTp(t.i,
                    SimpleDate.format(t.c),
                    SimpleDate.format(t.u),
                    t.v,t.s,t.m)
    val z = Extraction.decompose(n)    
    val line = Printer.compact(JsonAST.render(z))
    line
  }
  
  def parseTopic(line:String) = {
    val s = parse(line).extract[RTp]
    val t = new Topic
    t.i = s.i
    t.c = SimpleDate.parse(s.c)
    t.u = SimpleDate.parse(s.u)
    t.v = s.v
    t.s = s.s
    t.m = s.m
    t
  }
  
  def convertData(n:Topic) = {
    val x = ScalaMessagePack.write(n)
    x
  }
  
  def parseData(x:Array[Byte]) = {
    ScalaMessagePack.read[Topic](x)
  }
  
}

//object TestCt extends App {
//  
//  val t = new Topic
//  t.i = "123"
//  t.v = Map("1"->1.0,"2"->2.0)
//  t.s = 1.0
//  t.m = Map("e"->Array("xxx","zzz"),"t"->Array("x","z"))
//  
//  //val x = Converter.convertTopic(t)
//  //println(x)
//  //val z = Converter.parseTopic(x)
//  
//  val x = Converter.convertData(t)
//  val z = Converter.parseData(x)
//  println(z)
//  println(z.i)
//  
//}
